{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.8-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python37864bit9859aadc2f9b4163a401bc1d107b5e60",
   "display_name": "Python 3.7.8 64-bit"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "0\n1\n2\n3\n4\n"
    }
   ],
   "source": [
    "for i in range(5):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "(range(5, 10), range(0, 10, 3), range(-10, -100, -30))"
     },
     "metadata": {},
     "execution_count": 6
    }
   ],
   "source": [
    "'''\n",
    "给定的终止数值并不在要生成的序列里；range(10) 会生成10个值，并且是以合法的索引生成一个长度为10的序列。range也可以以另一个数字开头，或者以指定的幅度增加（甚至是负数；有时这也被叫做 '步进'）\n",
    "'''\n",
    "x = range(5,10)\n",
    "y = range(0,10,3)\n",
    "z = range(-10, -100, -30)\n",
    "x,y,z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "0 Mary\n1 had\n2 a\n3 little\n4 lamb\n"
    }
   ],
   "source": [
    "'''\n",
    "要以序列的索引来迭代，您可以将 range() 和 len() 组合如下:\n",
    "'''\n",
    "\n",
    "a = ['Mary', 'had', 'a', 'little', 'lamb']\n",
    "for i in range(len(a)):\n",
    "    print(i, a[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "range(0, 10)\n"
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "(None, range(0, 10))"
     },
     "metadata": {},
     "execution_count": 9
    }
   ],
   "source": [
    "'''\n",
    "然而，在大多数这类情况下，使用 enumerate() 函数比较方便，请参见 循环的技巧 。\n",
    "\n",
    "如果你只打印 range，会出现奇怪的结果:\n",
    "'''\n",
    "print(range(10)), range(0,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "6"
     },
     "metadata": {},
     "execution_count": 10
    }
   ],
   "source": [
    "sum(range(4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[0, 1, 2, 3]"
     },
     "metadata": {},
     "execution_count": 11
    }
   ],
   "source": [
    "list(range(4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "0 Mary\n1 had\n2 a\n3 little\n4 lamb\n"
    }
   ],
   "source": [
    "for i, a in enumerate(a):\n",
    "    print(i, a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}